package org.ovirt.microvirt.services.situation.impl;

import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.ovirt.microvirt.beans.Host;
import org.ovirt.microvirt.services.HostService;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class HostCheckJob {

	public HostCheckJob(JmsTemplate jmsTemplate, HostService hostService) {
		super();
		this.jmsTemplate = jmsTemplate;
		this.hostService = hostService;
	}

	final JmsTemplate jmsTemplate;
	final HostService hostService;

	public void checkAllHosts() {
		for(final Host host : hostService.list()) {
			jmsTemplate.send(new MessageCreator() {
				
				@Override
				public Message createMessage(final Session session) throws JMSException {
					final ObjectMessage message = session.createObjectMessage();
					message.setObject(host);
					message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
					message.setJMSExpiration(1000);
					return message;
				}
			});
		}
	}

}
